(define (atom? x)
  (and (not (pair? x))
       (not (null? x))))

;; (define (lat? l)
;;   (if (null? l)
;;     #t
;;     (if (atom? (car l))
;;       (lat? (cdr l))
;;       #f)))

(define (lat? l)
  (cond
    ((null? l) #t)
    ((atom? (car l)) (lat? (cdr l)))
    (else #f)))

(define (a-pair? x)
  (cond
    ((atom? x) #f)
    ((null? x) #f)
    ((null? (cdr x)) #f)
    ((null? (cddr x)) #f)
    (else #f)))

(define (add1 n) (+ n 1))
(define (sub1 n) (- n 1))

(define (member? a lat)
  (cond
    ((null? lat) #f)
    ((eq? a (car lat)) #t)
    (else (member? a (cdr lat)))))

(define (rember a lat)
  (cond
    ((null? lat) '())
    ((eq? a (car lat)) (cdr lat))
    (else (cons (car lat)
                (rember a (cdr lat))))))

(define (rember* a l)
  (cond
    ((null? l) '())
    ((atom? (car l))
     (if (eq? a (car l))
       (rember* a (cdr l))
       (cons (car l) (rember* a (cdr l)))))
    (else (cons (rember* a (car l))
                (rember* a (cdr l))))))

(define (firsts l)
  (if (null? l)
    '()
    (cons (caar l) (firsts (cdr l)))))

(define (insertR new old lat)
  (cond
    ((null? lat) #t)
    ((eq? old (car lat)) (cons old (cons new (cdr lat))))
    (else (cons (car lat) (insertR new old (cdr lat))))))

(define (o- m n)
  (if (zero? n)
    m
    (o- (sub1 m) (sub1 n))))

(define (rempick n lat)
  (if (zero? (sub1 n))
    (cdr lat)
    (cons (car lat)
          (rempick (sub1 n) (cdr lat)))))


(define first car)
(define second cadr)
(define third caddr)
(define (build s1 s2)
  (cons s1 (cons s2 '())))
